package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.user.domain.chat.Group;
import com.example.user.domain.chat.GroupMember;
import com.example.user.mapper.chat.GroupMapper;
import com.example.user.mapper.chat.GroupMemberMapper;
import com.example.user.service.IGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GroupServiceImpl implements IGroupService {
    private static final Logger logger = LoggerFactory.getLogger(GroupServiceImpl.class);

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Override
    public boolean addGroupMember(GroupMember groupMember) {
        // 检查群组是否存在
        Group group = getGroup(groupMember.getGroupId());
        if (group == null) {
            logger.warn("群组 {} 不存在或已删除", groupMember.getGroupId());
            return false;
        }

        // 检查用户是否已经是群成员
        LambdaQueryWrapper<GroupMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupMember::getGroupId, groupMember.getGroupId())
                .eq(GroupMember::getUserId, groupMember.getUserId())
                .eq(GroupMember::getDelFlag, 0);

        if (groupMemberMapper.selectCount(queryWrapper) > 0) {
            logger.warn("用户 {} 已经是群组 {} 的成员", groupMember.getUserId(), groupMember.getGroupId());
            return false;
        }

        // 保存群成员
        int result = groupMemberMapper.insert(groupMember);

        if (result > 0) {
            logger.info("添加群成员: 群组ID={}, 用户ID={}", groupMember.getGroupId(), groupMember.getUserId());
            return true;
        }
        return false;
    }

    @Override
    public boolean delGroupMember(GroupMember groupMember) {
        // 检查群组是否存在
        Group group = getGroup(groupMember.getGroupId());
        if (group == null) {
            logger.warn("群组 {} 不存在或已删除", groupMember.getGroupId());
            return false;
        }

        // 检查用户是否是该群组的成员
        LambdaQueryWrapper<GroupMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupMember::getGroupId, groupMember.getGroupId())
                .eq(GroupMember::getUserId, groupMember.getUserId())
                .eq(GroupMember::getDelFlag, 0);

        GroupMember existingMember = groupMemberMapper.selectOne(queryWrapper);
        if (existingMember == null) {
            logger.warn("用户 {} 不是群组 {} 的成员", groupMember.getUserId(), groupMember.getGroupId());
            return false;
        }

        // 执行逻辑删除
        existingMember.setDelFlag(1);
        int result = groupMemberMapper.updateById(existingMember);

        if (result > 0) {
            logger.info("删除群成员: 群组ID={}, 用户ID={}", groupMember.getGroupId(), groupMember.getUserId());
            return true;
        }
        return false;
    }

    private Group getGroup(Long groupId) {
        LambdaQueryWrapper<Group> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Group::getGroupId, groupId)
                .eq(Group::getDelFlag, 0);
        return groupMapper.selectOne(queryWrapper);
    }


    @Override
    public int addGroup(Group group){
        return groupMapper.insert(group);
    }
}
